package com.mftour.common.customLabel;

import java.util.List;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.mftour.common.cache.InitCache;
import com.mftour.moudles.entity.ConsolePermission;

public class ConfigPermissionTableTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1033225338010907426L;

	private List<ConsolePermission> allPermissionsList;

	private Set<Integer> rpSet;

	public List<ConsolePermission> getAllPermissionsList() {
		return allPermissionsList;
	}

	public void setAllPermissionsList(List<ConsolePermission> allPermissionsList) {
		this.allPermissionsList = allPermissionsList;
	}

	public Set<Integer> getRpSet() {
		return rpSet;
	}

	public void setRpSet(Set<Integer> rpSet) {
		this.rpSet = rpSet;
	}

	private String createTr(List<ConsolePermission> cps, int level, Integer parentId) {
		StringBuilder sb = new StringBuilder();
		for (ConsolePermission cp : cps) {
			sb.append("<tr id=\"permission" + cp.getPermissionId() + "\">");
			sb.append("<td align=\"center\" onclick=\"changeCheck('permissionId" + cp.getPermissionId()
					+ "')\"><input type=\"checkbox\" id=\"permissionId" + cp.getPermissionId()
					+ "\" name=\"permissionIds\" title=\"选择\" value=\"" + cp.getPermissionId() + "\"");
			if (rpSet.contains(cp.getPermissionId())) {
				sb.append(" checked=\"checked\"");
			}
			if(parentId!=null){
				sb.append(" parentId=\""+parentId+"\" onclick=\"checkParent(this,'"+parentId+"',true)\"");
			}else{
				sb.append("onclick=\"checkParent(this,null,true)\"");
			}
			sb.append("></td>");
			sb.append("<td>");
			for (int i = 0; i < level; i++) {
				sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			}
			sb.append(cp.getPermissionName());
			sb.append("</td>");
			sb.append("<td>");
			if (cp.getParentId().equals(0)) {
				sb.append("系统");
			} else {
				sb.append(InitCache.allCategoryMap.get(cp.getParentId()).getPermissionName());
			}
			sb.append("</td>");
			sb.append("<td>");
			switch (cp.getPermissionType()) {
			case "0":
				sb.append("<span class=\"label label-success label-normal\">目录</span>");
				break;
			case "1":
				sb.append("<span class=\"label label-warning label-normal\">页面</span>");
				break;
			case "2":
				sb.append("<span class=\"label label-primary label-normal\">按钮</span>");
				break;
			case "3":
				sb.append("<span class=\"label label-danger label-normal\">其他</span>");
				break;
			}
			sb.append("</td>");
			sb.append("<td>");
			sb.append(cp.getSort());
			sb.append("</td>");
			sb.append("<td>");
			sb.append(cp.getPermissionIcon());
			sb.append("</td>");

			sb.append("</tr>");
			if (cp.getPermissions() != null && cp.getPermissions().size() > 0) {
				sb.append(createTr(cp.getPermissions(), level + 1, cp.getPermissionId()));
			}
		}
		return sb.toString();
	}

	@Override
	public int doEndTag() throws JspException {
		if (allPermissionsList == null) {
			allPermissionsList = InitCache.allPermissionsList;
		}

		StringBuilder sb = new StringBuilder();
		sb.append(" <table class=\"table table-hover  table-bordered\">");
		sb.append(
				"<tbody><tr><th width=\"5%\" onclick=\"changeCheck('allClickTag')\">全选<input type=\"checkbox\" onchange=\"toggleAll()\" id=\"allClickTag\"></th><th width=\"30%\">权限名称</th> <th width=\"30%\">所属目录</th> <th width=\"5%\">权限类型</th>  <th width=\"5%\">排序</th>  <th width=\"5%\">图标</th></tr>");
		sb.append(createTr(allPermissionsList, 0, null));

		sb.append("</tbody></table>");
		try {
			pageContext.getOut().write(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		allPermissionsList = null;

		return super.doEndTag();
	}

}
